package top.kent90.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import top.kent90.common.object.Result;
import top.kent90.common.object.ZTreeNode;
import top.kent90.dao.SysRolesDao;
import top.kent90.pojo.SysPopedom;
import top.kent90.pojo.SysRoleRights;
import top.kent90.pojo.SysRoles;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(timeout = 60000)
public class SysRolesServiceImpl implements SysRolesService {
    @Autowired
    SysRolesDao dao;
    @Override
    public Result queryRoles(Map<String, Object> map, String pageNum, String pageSize) {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<SysRoles> sysRoles = dao.queryRoles(map);
        PageInfo pg = new PageInfo(sysRoles);
        return new Result("0","",pg.getTotal(),pg.getList());
    }
    /**
     * 生成树状菜单
     *
     * @param roleId
     * @return
     */
    @Override
    public List<ZTreeNode> createTree(String roleId)  {
        // 查询菜单权限项表
        List<SysPopedom> listLev = dao.selectSysPopedom();

        List<ZTreeNode> rtnList = new ArrayList<ZTreeNode>();
        try {
            for (SysPopedom spp : listLev) {
                ZTreeNode item = new ZTreeNode(spp.getItemName(), spp.getId()
                        .toString());
                Map<String, String> map = new HashMap<String, String>();
                map.put("popId", spp.getId().toString());
                map.put("roleId", roleId);
                int num = dao.selectnum1(map);
                if (num > 0) // 查询角色是否拥有此权限
                    item.setChecked(true);

                item.setIsParent(true);
                item.setOpen(true);
                // 查询二级菜单
                item.setChildren(createNodeLev1(spp.getMenuId().toString(), roleId));
                rtnList.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtnList;
    }

    /**
     * 查询二级节点
     *
     * @param menuId
     * @param roleId
     * @return
     * @throws Exception
     */
    public List<ZTreeNode> createNodeLev1(String menuId, String roleId)
            throws Exception {
        // 查询二级菜单
        List<Map<String, String>> listLev0 = dao.selectmap1(menuId);

        List<ZTreeNode> rtnList = new ArrayList<ZTreeNode>();
        for (Map<String, String> map : listLev0) {
            ZTreeNode item = new ZTreeNode(map.get("item_name").toString(), String.valueOf(map.get("id")));

            map.put("popId", String.valueOf(map.get("id")));
            map.put("roleId", roleId);
            int num = dao.selectnum1(map);
            if (num > 0) // 拥有权限在设置页面显示
                item.setChecked(true);
            item.setOpen(true); // 打开状态
            if (map.get("url") != null&& !"none".equals(map.get("url").toString())) {
                // 如果不是菜单 ，加上菜单功能项
                Map<String, String> param = new HashMap<String, String>();
                param.put("roleId", roleId);
                param.put("menuId", map.get("menu_id").toString());
                // 查询功能项
                List<Map<String, String>> ppList = dao.selectmap2(param);
                List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
                for (Map<String, String> map1 : ppList) {
                    Map<String, Object> ppMap = new HashMap<String, Object>();
                    ppMap.put("id", map1.get("id"));
                    ppMap.put("menuId", map1.get("menu_id"));
                    ppMap.put("itemCode", map1.get("item_code"));
                    ppMap.put("itemName", map1.get("item_name"));
                    if (map1.get("checked") != null)
                        ppMap.put("checked", map1.get("checked"));
                    mList.add(ppMap);
                }
                item.setModel(mList);
            } else {
                // 是菜�? 查询
                item.setIsParent(true);
                item.setOpen(true);
                item.setChildren(createNodeLev2(map.get("menu_id").toString(),roleId));
                // 查询下级菜单
            }
            rtnList.add(item);
        }
        return rtnList;
    }

    /**
     * 3级菜单查询
     *
     * @param menuId
     * @param roleId
     * @return
     * @throws Exception
     */
    public List<ZTreeNode> createNodeLev2(String menuId, String roleId)
            throws Exception {

        // 查询菜单
        List<Map<String, String>> listLev0 = dao.selectmap3(menuId);
        List<ZTreeNode> rtnList = new ArrayList<ZTreeNode>();
        for (Map<String, String> map : listLev0) {
            ZTreeNode item = new ZTreeNode(map.get("item_name").toString(), String.valueOf(map.get("id")));
            map.put("popId", String.valueOf(map.get("id")));
            map.put("roleId", roleId);
            int num = dao.selectnum1(map);
            if (num > 0) // 如过有权追加选中
                item.setChecked(true);
            item.setOpen(true); // 打开状
            if (map.get("url") != null
                    && !"none".equals(map.get("url").toString())) {
                // 加上菜单功能项目
                Map<String, String> param = new HashMap<String, String>();
                param.put("roleId", roleId);
                param.put("menuId", map.get("menu_id")); // 查询功能
                List<Map<String, String>> ppList = dao.selectmap2(param);

                List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
                for (Map<String, String> map1 : ppList) {

                    Map<String, Object> ppMap = new HashMap<String, Object>();
                    ppMap.put("id", map1.get("id"));
                    ppMap.put("menuId", map1.get("menu_id"));
                    ppMap.put("itemCode", map1.get("item_code"));
                    ppMap.put("itemName", map1.get("item_name"));
                    if (map1.get("checked") != null)
                        ppMap.put("checked", map1.get("checked"));
                    mList.add(ppMap);
                }
                item.setModel(mList);
            } else {
                // 如果是菜单 继续递归查询追加
                item.setIsParent(true);
                item.setOpen(true);
                item.setChildren(createNodeLev2(map.get("menu_id").toString(),roleId));
            }
            rtnList.add(item);
        }
        return rtnList;
    }
    @Override
    public int savePopedomRoles(Integer roleId, List<SysRoleRights> list) {
        //1.删除当前角色原有的权限
        dao.delPopedomRoles(roleId);

        //2.保存新的权限
        int rst = dao.savePopedomRoles(list);
        return rst;
    }

    @Override
    public List<Map<String, Object>> queryRoleUser(Integer roleId) {
        List<Map<String,Object>> rtnList = dao.queryRoleUser(roleId);
        return rtnList;
    }

    @Override
    public int delRole(String roleid) {
        //删除角色
        int rst =dao.delRole(Integer.parseInt(roleid));
        //删除角色对应的权限项
        dao.delPopedomRoles(Integer.parseInt(roleid));
        return rst;
    }

    @Override
    public int addRole(SysRoles sysRole) {
       int rst =  dao.addRole(sysRole);
        return rst;
    }

}
